package compute

import (
	"gitee.com/amoyx/cloud-provider/pkg/cloud/tencent/models"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	qcvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
)

type RegionGreeter interface {
	Region() RegionInterface
}

type RegionInterface interface {
	List() (list []models.RegionResponse)
	ZoneList(region string) (list []models.ZoneResponse)
}

type region struct {
	compute *Compute
}

func newRegion(compute *Compute) RegionInterface {
	return &region{
		compute: compute,
	}
}

func (r *region) List() (list []models.RegionResponse) {
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = string(CvmEndPoint)
	client, _ := qcvm.NewClient(r.compute.Credential(), "", cpf)
	request := qcvm.NewDescribeRegionsRequest()
	response, err := client.DescribeRegions(request)
	if err != nil {
		return
	}
	for _, val := range response.Response.RegionSet {
		list = append(list, models.RegionResponse{
			Region:      *val.Region,
			RegionName:  *val.RegionName,
			RegionState: *val.RegionState,
		})
	}
	return
}

func (r *region) ZoneList(city string) (list []models.ZoneResponse) {
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = string(CvmEndPoint)
	client, _ := qcvm.NewClient(r.compute.Credential(), city, cpf)
	request := qcvm.NewDescribeZonesRequest()
	response, err := client.DescribeZones(request)
	if err != nil {
		return
	}
	for _, val := range response.Response.ZoneSet {
		list = append(list, models.ZoneResponse{
			Zone:      *val.Zone,
			ZoneId:    *val.ZoneId,
			ZoneName:  *val.ZoneName,
			ZoneState: *val.ZoneState,
		})
	}
	return
}
